function CArray(numElements) {
    this.dataStore = [];
    this.pos = 0;
    this.numElements = numElements;
    this.insert = insert;
    this.toString = toString;
    this.clear = clear;
    this.setData = setData;
    this.swap = swap;

    this.bubbleSort = bubbleSort;
    this.selectionSort = selectionSort;
    this.insertionSort = insertionSort;
    this.shellSort = shellSort;

    this.gaps = [5, 3, 1];
    this.setGaps = setGaps;

    for (var i = 0; i < numElements; ++i) {
        this.dataStore[i] = i;
    }
}
function setData() {
    for (var i = 0; i < this.numElements; ++i) {
        this.dataStore[i] = Math.floor(Math.random() * (this.numElements + 1));
    }
}
function clear() {
    for (var i = 0; i < this.dataStore.length; ++i) {
        this.dataStore[i] = 0;
    }
}
function insert(element) {
    this.dataStore[this.pos++] = element;
}
function toString() {
    var retstr = "";
    for (var i = 0; i < this.dataStore.length; ++i) {
        retstr += this.dataStore[i] + " ";
        if (i > 0 & i % 10 == 0) {
            retstr += "\n";
        }
    }
    return retstr;
}
function swap(arr, index1, index2) {
    var temp = arr[index1];
    arr[index1] = arr[index2];
    arr[index2] = temp;
}

function setGaps(_gaps) {
    this.gaps = _gaps;
}
function print(x) {
    console.log(x);
}

(function () {
    var numElements = 100;
    var myNums = new CArray(numElements);
    myNums.setData();
    print(myNums.toString());
}());

/**
 * 冒泡排序
 */
function bubbleSort() {
    var numElements = this.dataStore.length;
    var temp;
    for (var outer = numElements; outer >= 2; --outer) {
        for (var inner = 0; inner <= outer - 1; ++inner) {
            if (this.dataStore[inner] > this.dataStore[inner + 1]) {
                swap(this.dataStore, inner, inner + 1);
            }
        }
        print(this.toString());
    }
}

(function () {
    var numElements = 10;
    var mynums = new CArray(numElements);
    mynums.setData();
    print(mynums.toString());
    mynums.bubbleSort();
    print('bubble');
    print(mynums.toString());
}());

/**
 * 选择排序
 */
function selectionSort() {
    var min, temp;
    for (var outer = 0; outer <= this.dataStore.length - 2; ++outer) {
        min = outer;
        for (var inner = outer + 1; inner <= this.dataStore.length - 1; ++inner) {
            // print(this.dataStore[inner] +' vs '+ this.dataStore[min] + (this.dataStore[inner] < this.dataStore[min]));
            if (this.dataStore[inner] < this.dataStore[min]) {
                min = inner;
            }
        }
        swap(this.dataStore, outer, min);
        print(this.toString());
    }
}

(function () {
    var numElements = 10;
    var mynums = new CArray(numElements);
    mynums.setData();
    print('selection');
    print(mynums.toString());
    mynums.selectionSort();
    print('-----result');
    print(mynums.toString());
}());

/**
 * 插入排序
 */
function insertionSort() {
    var temp, inner;
    for (var outer = 1; outer <= this.dataStore.length - 1; ++outer) {
        temp = this.dataStore[outer];
        inner = outer;
        while (inner > 0 && (this.dataStore[inner - 1] >= temp)) {
            this.dataStore[inner] = this.dataStore[inner - 1];
            --inner;
        }
        this.dataStore[inner] = temp;

        print(this.toString());
    }
}

(function () {
    var numElements = 10;
    var mynums = new CArray(numElements);
    mynums.setData();
    print('insertionSort');
    print(mynums.toString());
    mynums.insertionSort();
    print('-----result');
    print(mynums.toString());
}());

/**
 * 希尔排序
 * 希尔排序的工作原理是,通过定义一个间隔序列来表示在排序过程中进行比较的元素之间有多远的间隔
 */
function shellSort() {
    for (var idx = 0; idx < this.gaps.length; ++idx) {
        for (var gap = this.gaps[idx]; gap < this.dataStore.length; ++gap) {
            var temp = this.dataStore[gap];
            for (var j = gap; j >= this.gaps[idx] && this.dataStore[j - this.gaps[idx]] > temp; j -= this.gaps[idx]) {
                this.dataStore[j] = this.dataStore[j - this.gaps[idx]];
            }
            this.dataStore[j] = temp;
        }
    }
}

(function () {
    var numElements = 10;
    var mynums = new CArray(numElements);
    mynums.setData();
    print('shellSort');
    print(mynums.toString());
    mynums.shellSort();
    print('-----result');
    print(mynums.toString());
}());
